import 'package:flutter/material.dart';

class User {
  Image country;
  int golden;
  int silver;
  int copper;
  int total;

  User(this.country, this.golden, this.silver, this.copper, this.total);
}

class Olympic08 extends StatefulWidget {
  Olympic08({Key? key}) : super(key: key);

  @override
  _Olympic08State createState() => _Olympic08State();
}

class _Olympic08State extends State<Olympic08> {
  List<User> data = [
    User(Image.asset("assets/images/olympic/08/America.jfif", width: 50, height: 34, fit: BoxFit.fill), 39, 41, 33, 113),
    User(Image.asset("assets/images/olympic/08/China.jfif", width: 50, height: 34, fit: BoxFit.fill), 38, 32, 18, 88),
    User(Image.asset("assets/images/olympic/08/Japan.jfif", width: 50, height: 34, fit: BoxFit.fill), 27, 14, 17, 58),
    User(Image.asset("assets/images/olympic/08/England.jfif", width: 50, height: 30, fit: BoxFit.fill), 22, 21, 22, 65),
    User(Image.asset("assets/images/olympic/08/Russia.png", width: 50, height: 34, fit: BoxFit.fill), 20, 28, 23, 71),
    User(Image.asset("assets/images/olympic/08/Australia.jfif", width: 50, height: 31, fit: BoxFit.fill), 17, 7, 22, 46),
    User(Image.asset("assets/images/olympic/08/Netherlands.jfif", width: 50, height: 34, fit: BoxFit.fill), 10, 12, 14, 36),
    User(Image.asset("assets/images/olympic/08/France.jfif", width: 50, height: 34, fit: BoxFit.fill), 10, 12, 11, 33),
    User(Image.asset("assets/images/olympic/08/Germany.jfif", width: 50, height: 34, fit: BoxFit.fill), 10, 11, 16, 37),
    User(Image.asset("assets/images/olympic/08/Italy.jfif", width: 50, height: 34, fit: BoxFit.fill), 10, 10, 20, 40),
  ];

  var _sortAscending = true;
  var _sortColumnIndex = 0;

  List<DataRow> _getUserRows() {
    List<DataRow> dataRows = [];
    for (int i = 0; i < data.length; i++) {
      dataRows.add(
          DataRow(
              cells: [
                DataCell(data[i].country),
                DataCell(Text('${data[i].golden}')),
                DataCell(Text('${data[i].silver}')),
                DataCell(Text('${data[i].copper}')),
                DataCell(Text('${data[i].total}')),
              ]
          )
      );
    }

    return dataRows;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: SingleChildScrollView(
          scrollDirection: Axis.vertical,
          child: DataTable(
            sortColumnIndex: _sortColumnIndex,
            sortAscending: _sortAscending,
            dataRowHeight: 60,
            horizontalMargin: 20,
            columnSpacing: 27,
            columns: [
              DataColumn(label: Text('国家/地区')),
              DataColumn(
                  label: Text('金牌'),
                  numeric: true,
                  onSort: (int columnIndex, bool asscending) {
                    setState(() {
                      _sortColumnIndex = columnIndex;
                      _sortAscending = asscending;
                      if (asscending) {
                        data.sort((a, b) => a.golden.compareTo(b.golden));
                      } else {
                        data.sort((a, b) => b.golden.compareTo(a.golden));
                      }
                    });
                  }
              ),
              DataColumn(
                  label: Text('银牌'),
                  numeric: true,
                  onSort: (int columnIndex, bool asscending) {
                    setState(() {
                      _sortColumnIndex = columnIndex;
                      _sortAscending = asscending;
                      if (asscending) {
                        data.sort((a, b) => a.silver.compareTo(b.silver));
                      } else {
                        data.sort((a, b) => b.silver.compareTo(a.silver));
                      }
                    });
                  }
              ),
              DataColumn(
                  label: Text('铜牌'),
                  numeric: true,
                  onSort: (int columnIndex, bool asscending) {
                    setState(() {
                      _sortColumnIndex = columnIndex;
                      _sortAscending = asscending;
                      if (asscending) {
                        data.sort((a, b) => a.copper.compareTo(b.copper));
                      } else {
                        data.sort((a, b) => b.copper.compareTo(a.copper));
                      }
                    });
                  }
              ),
              DataColumn(
                  label: Text('总计'),
                  numeric: true,
                  onSort: (int columnIndex, bool asscending) {
                    setState(() {
                      _sortColumnIndex = columnIndex;
                      _sortAscending = asscending;
                      if (asscending) {
                        data.sort((a, b) => a.total.compareTo(b.total));
                      } else {
                        data.sort((a, b) => b.total.compareTo(a.total));
                      }
                    });
                  }
              ),
            ],
            rows: _getUserRows(),
          )
      ),
    );
  }
}